<?php
/*
  Plugin Name: Registration
  Plugin URI: http://www.techstorm.com
  Description: Registration Plugin
  Version: 1
  Author: TechStorm
  Author URI: http://www.techstorm.com
 */

add_action('register_form', 'registration_register_form');

function registration_register_form() {
    $first_name = ( isset($_POST['first_name']) ) ? $_POST['first_name'] : '';
    ?>
    <p>
        <label for="first_name"><?php _e('First name', 'mydomain') ?><br />
            <input type="text" 
                   name="first_name" 
                   id="first_name" 
                   class="input" 
                   value="<?php echo esc_attr(stripslashes($first_name)); ?>" 
                   size="25" />
        </label>
    </p>
    <?php
}

add_filter('registation_errors', 'registration_registration_errors', 10, 3);

function registration_registration_errors($error, $sanitized_user_login, $user_email) {

    if (empty($_POST['first_name']))
        $errors->add('first_name_error', __('<strong>ERROR</strong>: You must include a first name.', 'mydomain'));

    return $errors;
}

//3. Finally, save our extra registration user meta.
add_action('user_register', 'myplugin_user_register');

function myplugin_user_register($user_id) {
    if (isset($_POST['first_name']))
        update_user_meta($user_id, 'first_name', $_POST['first_name']);
}

function HttpPost($postUrl_, $nvpStr_) {
    $url = $postUrl_;

    // Set the curl parameters.
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    // Set the request as a POST FIELD for curl.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpStr_);

    // Get response from the server.
    $httpResponse = curl_exec($ch);

    if (!$httpResponse) {
        exit("$methodName_ failed: " . curl_error($ch) . '(' . curl_errno($ch) . ')');
    }
    curl_close($ch);

    return $httpResponse;
}

function request_for_payment($url_, $code_, $rechargeMinutes_, $ref_, $id1_, $id2_) {
    $encode = urlencode($code_);
    $recMinutes = urlencode($rechargeMinutes_);
    $ref = urlencode($ref_);
    $id1 = urlencode($id1_);
    $id2 = urlencode($id2_);
    $nvpStr = "&code=$encode&rechargeMinutes=$recMinutes&ref=$ref&id1=$id1&id2=$id2";
    $httpParsedResponseAr = HttpPost($url_, $nvpStr);
    echo $httpParsedResponseAr;
}

function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $user = new WP_User($user_id);

    $user_login = stripslashes($user->user_login);
    $user_email = stripslashes($user->user_email);

    // The blogname option is escaped with esc_html on the way into the database in sanitize_option
    // we want to reverse this for the plain text arena of emails.
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    $messageAdmin = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
    $messageAdmin .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
    $messageAdmin .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";

    //wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);

    if (empty($plaintext_pass))
        return;
    $messageUser = sprintf(__('Registration completely! Below are your login infomation:')) . "\r\n";
    $messageUser .= sprintf(__('Username: %s'), $user_login) . "\r\n";
    $messageUser .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
    $messageUser .= wp_login_url() . "\r\n";

//    wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
    $admess = urlencode($messageAdmin);
    $usmess = urlencode($messageUser);
    $pw = urlencode($plaintext_pass);
    $us = urlencode($user_login);
    $usmail = urlencode($user_email);
    $admail = urlencode($messageUser);
    $site_title = urlencode($blogname);
    
    $nvpStr = "&us=$us&pw=$pw&usmess=$usmess&admess=$admess&usmail=$usmail&admail=$admail&site_title=$site_title";
    
    $home_url = get_home_url();
    $url = $home_url . '/paypal/paypal.php';
    $httpParsedResponseAr = HttpPost($url, $nvpStr);
    echo $httpParsedResponseAr;
    
//    wp_redirect('project01/paypal/paypal.php');
    
    
//            $mail = new \PHPMailer();
//            $mail->IsSMTP();
//            $mail->SMTPDebug = 0;
//            $mail->SMTPAuth = 'login';
//            $mail->SMTPSecure = 'ssl';
//            $mail->Host = 'smtp.gmail.com'; // <- Server email
//            $mail->Port = 465; // <- Port của server
//            $mail->Username = 'vietanh.pd1@gmail.com'; // <- Email của người gửi
//            $mail->Password = 'nguyenthuydan'; // <- Pass của email người gửi
//            $mail->SetFrom('vietanh.pd1@gmail.com', 'Việt Anh'); // <- Email của người gửi + tên người gửi
//            $mail->Subject = 'Test'; // <- Tiêu đề email
//            $mail->Body = 'The content'; // <- Nội dung email
//            $mail->AddAddress('vietanh.sgu@gmail.com'); // <- Email của người nhận
////            $path = "C://RHDSetup.log";
////            $mail->addAttachment($path); // <- $path là đường dẫn tới file cần đính kèm
//            $mail->Send();
            exit;
}

?>